/*
 * SunSpotApplication.java
 *
 * Created on Dec 5, 2011 5:23:24 PM;
 */
package org.sunspotworld;

import com.sun.spot.peripheral.radio.RadioFactory;
import com.sun.spot.resources.Resources;
import com.sun.spot.resources.transducers.ISwitch;
import com.sun.spot.resources.transducers.ITriColorLED;
import com.sun.spot.resources.transducers.ITriColorLEDArray;
import com.sun.spot.resources.transducers.LEDColor;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.service.BootloaderListenerService;
import com.sun.spot.util.IEEEAddress;
import com.sun.spot.util.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import rpc.connection.StreamedRPC;
import rpc.messenger.VoltageMessenger;
import sensor.spot.SpotADC;
import sensor.telemetry.RPCSPOTAccelerometer;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class SunSpotApplication extends MIDlet {

    protected void startApp() throws MIDletStateChangeException {
        System.out.println("Hello, World!");
        
        InputStream in = null;
        OutputStream out = null;
        try {
            in = Connector.openInputStream("serial://usb");
            out = Connector.openOutputStream("serial://usb");
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Failed usb connection");
//            System.exit(0);
        }
        StreamedRPC rpcConn = new StreamedRPC(in, out);
        rpcConn.start();

//            SpotADC a0 = new SpotADC(0, 50, "a0");
//            a0.start();
//
//            VoltageMessenger m = new VoltageMessenger(54, a0, rpcConn);
//            m.startListening();

        RPCSPOTAccelerometer s = new RPCSPOTAccelerometer(rpcConn, new int[]{66, 67, 68}, 20);
        s.start();


        ITriColorLED led = EDemoBoard.getInstance().getLEDs()[0];
        LEDColor l = new LEDColor(0, 50, 0);
        led.setOn();
        led.setColor(l);
        while (true) {
            led.setOn();
            Utils.sleep(50);
            led.setOff();
            Utils.sleep(500);
        }


        //        System.out.println("Hello, world");
        //        BootloaderListenerService.getInstance().start();   // monitor the USB (if connected) and recognize commands from host
        //
        //        long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
        //        System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
        //
        //        ISwitch sw1 = (ISwitch) Resources.lookup(ISwitch.class, "SW1");
        //        ITriColorLED led = leds.getLED(0);
        //        led.setRGB(100,0,0);                    // set color to moderate red
        //        while (sw1.isOpen()) {                  // done when switch is pressed
        //            led.setOn();                        // Blink LED
        //            Utils.sleep(250);                   // wait 1/4 seconds
        //            led.setOff();
        //            Utils.sleep(1000);                  // wait 1 second
        //        }
        //        notifyDestroyed();                      // cause the MIDlet to exit

    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true the MIDlet must cleanup and release all resources.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }
}
